<h1>集計レポート(<%= @place_holder['YEAR'] + "/" +  @place_holder['MONTH'] + "/" +  @place_holder['DAY'] %>)</h1>
<p style="color: green"><%= flash[:notice] %></p>

<table class="report">
<%
# Collect
survey_properties = SurveyProperty.find_all_by_survey_id(@survey.id,
  :order => 'view_order')
columns = []
data_types = Hash.new
sums = Hash.new
survey_properties.each do |rp|
  columns << rp.ocr_name_full
  data_types.store(rp.ocr_name, rp.data_type)
  sums.store(rp.ocr_name, 0)
end

line_num = 1
%>
  <tr>
    <th class="manage"></th>
    <th class="name">調査対象</th>
    <% for column in columns do %>
    <th><%= column %></th>
    <% end %>
  </tr>
<% @answer_sheets.each do |a| %>
  <tr <%= ((line_num % 2) == 1) ? "class='dark'" : "" %>>
    <td class="manage"><%= line_num %></td>
    <td class="name"><%= a.candidate.candidate_name %><br><%= a.candidate.tel_number %>
	<% if a.date != nil %>
	<br>
		(<%= a.date.to_s(:date_nomal) %>)
	<% end %>
	</td>

  <% for column in columns do %>
    <%
    rp = AnswerSheetProperty.find(:first,
      :conditions => ['answer_sheet_id = ? and ocr_name = ?', a.id, column])
    cell_str = rp != nil ? rp.ocr_value : '-'
    cell_img = rp != nil ? rp.ocr_image : nil

    if data_types[column] == 'number' && cell_str =~ /^[0-9]+$/
      sums[column] = sums[column] + cell_str.to_i
    end

    if data_types[column] == 'image' || @all_image
		if cell_img != nil
			cell_item = '<img src="' + @prefix_image + '/' +cell_img + '" height="30"/>'
		else
			cell_item = '-'
		end
    else
		if cell_str != ""
			cell_item = cell_str
		else
			cell_item = '&nbsp;'
		end
      %>
    <% end %>
    <td align="center"><%= cell_item %></td>
  <% end %>
  </tr>
  <% line_num = line_num + 1 %>
<% end %>

<!-- Sum -->
<% unless @all_image %>
<tr class="summary">
  <td class="manage">計</td>
  <td></td>

  <% for column in columns do %>
    <% cell_item = (data_types[column] == 'number') ? sums[column] : '&nbsp;' %>
    <td align="center"><%= cell_item %></td>
  <% end %>
  </tr>
<% end %>
</table>
